package com.example.sensor.slice;

import com.example.sensor.ResourceTable;
import com.example.sensor.Utils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.location.Location;
import ohos.location.Locator;
import ohos.location.LocatorCallback;
import ohos.location.RequestParam;

public class LocatorAbilitySlice extends AbilitySlice {

    // 文本组件，用于显示地理位置
    private Text mTextLocation;
    // 地理定位“总管家”
    private Locator mLocator;
    // 定位回调
    private LocatorCallback mLocatorCallback;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_locator);
        mTextLocation = (Text) findComponentById(ResourceTable.Id_text_location);
        // 初始化地理定位“总管家”和定位回调
        mLocator = new Locator(this);
        mLocatorCallback = new LocatorCallback() {
            @Override
            public void onLocationReport(final Location location) {
                // 将地理位置更新到用户界面中
                getUITaskDispatcher().asyncDispatch(() -> {
                    mTextLocation.setText("经度:" + location.getLongitude()
                            + "纬度:" + location.getLatitude());
                });
            }

            @Override
            public void onStatusChanged(int i) {
            }

            @Override
            public void onErrorReport(int i) {
            }
        };
        // 导航场景RequestParam
        RequestParam requestParam = new RequestParam(RequestParam.SCENE_NAVIGATION);
        // 开始定位
        mLocator.startLocating(requestParam, mLocatorCallback);
    }

    @Override
    protected void onStop() {
        super.onStop();
        // 停止定位
        mLocator.stopLocating(mLocatorCallback);
    }
}
